package com.saber.king.controller;

import com.saber.king.aop.annotation.MyLog;
import com.saber.king.constants.Constant;
import com.saber.king.entity.Company;
import com.saber.king.service.CompanyService;
import com.saber.king.utils.DataResult;
import com.saber.king.utils.JwtTokenUtil;
import com.saber.king.vo.req.CompanyAddReqVO;
import com.saber.king.vo.req.CompanyPageReqVO;
import com.saber.king.vo.req.CompanyUpdateReqVO;
import com.saber.king.vo.resp.PageRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Saber污妖王
 * TODO: 公司模块相关接口
 * @version 0.0.1
 * @editor Saber污妖王
 * @project customer-circulation
 * @date 2020/6/23
 * @package com.saber.king.controller
 */
@RestController
@RequestMapping("/api")
@Api(tags = "客户管理-公司管理")
public class CompanyController {
    @Resource
    private CompanyService companyService;

    @PostMapping("/companies")
    @ApiOperation("分页获取公司信息接口")
    @RequiresPermissions("sys:company:list")
    @MyLog(title = "文件系统-公司管理", action = "分页获取公司信息接口")
    public DataResult<PageRespVO<Company>> pageInfo(@RequestBody CompanyPageReqVO vo) {
        return DataResult.success(companyService.pageInfo(vo));
    }

    @PostMapping("/company")
    @ApiOperation("新增公司接口")
    @RequiresPermissions("sys:company:add")
    @MyLog(title = "文件系统-公司管理", action = "新增公司接口")
    public DataResult<Object> addCompany(@RequestBody @Valid CompanyAddReqVO vo, HttpServletRequest request) {
        String userId = JwtTokenUtil.getUserId(request.getHeader(Constant.ACCESS_TOKEN));
        companyService.addCompany(vo, userId);
        return DataResult.success();
    }

    @PutMapping("/company")
    @ApiOperation("编辑公司接口")
    @RequiresPermissions("sys:company:update")
    @MyLog(title = "文件系统-公司管理", action = "编辑公司接口")
    public DataResult<Object> updateCompany(@RequestBody @Valid CompanyUpdateReqVO vo, HttpServletRequest request) {
        String userId = JwtTokenUtil.getUserId(request.getHeader(Constant.ACCESS_TOKEN));
        companyService.updateCompany(vo, userId);
        return DataResult.success();
    }

    @DeleteMapping("/company")
    @ApiOperation("删除公司接口")
    @RequiresPermissions("sys:company:delete")
    @MyLog(title = "文件系统-公司管理", action = "删除公司接口")
    public DataResult<Object> deleteCompany(@RequestBody List<String> list, HttpServletRequest request) {
        String userId = JwtTokenUtil.getUserId(request.getHeader(Constant.ACCESS_TOKEN));
        companyService.batchDeleteCompany(list, userId);
        return DataResult.success();
    }
}
